<?php
/**
 * Created by PhpStorm.
 * User: xiaoxin
 * Date: 2018/10/31 0031
 * Time: 9:05
 */

namespace app\index\controller;
use app\index\model\Collect;
use app\index\model\Good;
use app\index\model\Question;
use app\index\model\Reply;
use think\Controller;
use app\index\model\Cate;
use think\Db;

class Nom extends Controller
{
	/**
	 * ------帖子主页
	 * @return mixed
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function index(){

		$cate=Db::name('cate')
			->select();
		$this->assign('cate',$cate);

		/**
		 * 自动增加帖子的浏览量
		 */
		Db::name('question')->where('question_id', input('qid'))->cache(true,60)->setInc('view_num');
		/**
		 *查询路径上传过来的问题
		 */
		$question = Db::name('question')
			->alias('t1')
			->field('t1.*,t2.face,t2.nickname,t3.cate_name,t3.cate_id')
			->join('user t2','t1.user_id = t2.user_id')
			->join('cate t3','t1.cate_id = t3.cate_id')
			->where('question_id',input('qid'))
			->find();
		$this->assign('ques',$question);

		/**
		 * 查询该帖子的回复
		 */
		$reply = Db::name('reply')
			->alias('t1')
			->field('t2.face,t1.reply_id,t2.nickname,t2.user_id,t1.zan_num,t1.create_time,t1.content')
			->join('user t2','t1.user_id = t2.user_id')
			->where('question_id',input('qid'))
			->order('t1.create_time desc')
			->select();
		$this->assign('reply',$reply);

		/**
		 * 查询该用户是否有收藏该帖子
		 */
		$collect = Db::name('collect')
			->alias('t1')
			->field('t1.collect_id')
			->join('user t2','t1.user_id = t2.user_id')
			->join('question t3','t1.question_id = t3.question_id')
			->where('t3.question_id',input('qid'))
			->where('t2.user_id',session('user_id'))
			->find();
		$this->assign('collect',$collect);

		/**
		 * 按回复量排序取10条
		 */
		$hotReply = Question::order('reply_num desc')->limit(10)->select();
		$this->assign('hotReply',$hotReply);

		$this->assign('title', $question['title']);
		return $this->fetch();
	}


	/**
	 * 回复问题方法
	 * @throws \think\Exception
	 */
	public function reply(){
		$post = input('post.');
		if (!session('user_id')){
			$this->error('请先登陆');
		}
		$post['user_id'] = session('user_id');
		$post['create_time'] = time();
		$res = Reply::create($post);
		if ($res){
			Db::name('question')->where('question_id', $post['question_id'])->setInc('reply_num');
			$this->success('回复成功');
		}else{
			$this->error('回复失败');
		}
	}

	/**
	 * 收藏问题方法
	 */
	public function collect(){
		if (!session('user_id')){
			$this->error('请先登陆');
		}
		$post = input('post.');
		$res = Db::name('collect')
			->alias('t1')
			->join('user t2','t1.user_id = t2.user_id')
			->join('question t3','t1.question_id = t3.question_id')
			->where('t3.question_id',$post['question_id'])
			->where('t2.user_id',session('user_id'))
			->find();
		if ($res){
			$ok = Collect::where(['question_id'=>$post['question_id'],'user_id'=>session('user_id')])->delete();
			if ($ok){
				$this->success('取消收藏成功');
			}else{
				$this->error('取消收藏出现错误，请稍后再试');
			}
		}else{
			$post['user_id'] = session('user_id');
			$post['collect_time'] = time();
			$res = Collect::create($post);
			if ($res){
				$this->success('已收藏');
			}else{
				$this->error('收藏失败，请稍后再试');
			}
		}
	}

	/**
	 * 点赞方法
	 * @throws \think\Exception
	 */
	public function like(){
		if (!session('user_id')){
			$this->error('请先登陆');
		}
		$reply = input('post.');
		$res = Good::where(['user_id'=>session('user_id'),'reply_id'=>$reply['reply_id']])->find();
		if ($res){
			$this->error('你已经点过赞了');
		}
		$reply['user_id'] = session('user_id');
		$reply['good_time'] = time();
		$res = Good::create($reply);
		if ($res){
			$res = Db::name('reply')->where('reply_id', $reply['reply_id'])->setInc('zan_num');
			if (!$res){
				$this->error('出现错误,请稍后再试');
			}
			$this->success('已点赞');
		}else{
			$this->error('出现错误，请稍后再试');
		}
	}
}